<?php

/***  DOCUMENTATION LAYER

Last Update: Jul 2007
	
DESCRIPTION
	a function to measure memory usage
	
NOTES
	
	On Windows, this requires the tasklist executable.  XP Home does not come with 
	this, but it can be downloaded here: http://www.computerhope.com/download/winxp.htm
	
***/

/* -- EDIT THIS SECTION ----------------------------------------------------- */
// Some Meta Data
$page_title = 'PHP Sandbox';
$test_name = 'Memory Usage Test';
$run_procedure = 1;
$run_function = 0;
$num_cycles = 10;		// num of cycles to run function

// Include Files
#require_once('path/fname.php');

/* -- END SECTION ----------------------------------------------------------- */



/* -- NO EDIT --------------------------------------------------------------- */
// Timer
$_tx0 = microtime();
// HEADER
$buffer = "<h1 style='margin:8px 0 0;'>$page_title</h1>";
$buffer .= "<h4 style='margin:0 0 16px;'>$test_name</h4>";	// put script name here
/* -- END SECTION ----------------------------------------------------------- */


function memory_get_usage($echo=0)
{
	$mb_usage = '';		// return
	
	static $last_measure = 0;
	
	$_OUT = array();

	// Windows (requires tasklist.exe in C:\WINDOWS\system32)
	// to download: http://www.computerhope.com/download/winxp.htm
	if ( substr(PHP_OS,0,3) == 'WIN' )
	{
		exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $_OUT );
		if ( isset($_OUT[5]) )
		{
			$usage = preg_replace( '/[\D]/', '', $_OUT[5] ) * 1024;
			$mb_usage = round(($usage / 1024 / 1024), 3);
		}
		else
		{
			trigger_error('tasklist failed to exec');
		}
	}
	
	// Unix
	else
	{
		$pid = getmypid(); 
		exec("ps -eo%mem,rss,pid | grep $pid", $_OUT); 
		$output = explode("  ", $_OUT[0]);
		$usage = $_OUT[1] * 1024; 
		$mb_usage = round(($usage / 1024 / 1024), 3); 
	}
	
	if ( !empty($last_measure) && $echo )
	{
		$m3m = $mb_usage - $last_measure; 
		$m3k = $m3m * 1000;
		echo "<pre style='color:#c33;'>memory usage: $last_measure -> $mb_usage | difference: $m3m MB, $m3k KB</pre>";	
	}
	$last_measure = $mb_usage;
	
	if ( $echo ) echo "<pre>mem usage in MB: <b>$mb_usage</b></pre>"; 
	return $mb_usage;
}

/* -- EDIT THIS SECTION ----------------------------------------------------- */
// Procedural Testing

if ( $run_procedure ) 
{
	$buffer .= "<h4 style='margin:0; color:blue;'>Testing in Progess</h4>";
	
	echo "<h4>get memory usage at script start</h4>";
	memory_get_usage(1);
	
	// load a huge array
	$BIG_ARRAY = array();
	for ( $i=0; $i<=100000; $i++ )
	{
		$r = mt_rand(10000, 1000000);
		$BIG_ARRAY[] = $r;
	}
	
	// usage now
	echo "<h4>get memory usage after loading big array</h4>";
	memory_get_usage(1);
	
	// drop array
	$BIG_ARRAY = array();
	echo "<h4>clear array and measure again</h4>";
	memory_get_usage(1);
}

/* -- END SECTION ----------------------------------------------------------- */



/* -- EDIT THIS SECTION ----------------------------------------------------- */
// Function Test
if ( $run_function )
{

// Add your function here
function function_name() 
{
// *** DATA

// *** MANIPULATE

// *** RETURN

} // end function


	// run function
	$buffer .= "<h2 style='margin:0; color:green;'>Function Test</h2>";	
	while ( $num_cycles )
	{
		// $result = function_name();
		$buffer .= "<h6 style='margin:0; color:#ccc;'>test #{$run} complete</h6>";
		$num_cycles--;
	}
}

/* -- END SECTION ----------------------------------------------------------- */





/* -- NO EDITING NEEDED BELOW THIS LINE-------------------------------------- */
// output page

// Footer
$_txn = microtime();
$stx = number_format(((substr($_txn,0,9)) + (substr($_txn,-10)) - (substr($_tx0,0,9)) - (substr($_tx0,-10))),4); 
$footer = <<<HTML
<h4 style="margin:0; position:absolute; top:6px; right:6px; font-family:monospace; color:#36f; padding:4px; border:1px solid #36f; font-weight:normal; background:#f3f6ff;">
script completed in $stx s
</h4>
HTML;
$buffer .= $footer;

// Dump Html
$html = <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>PHP Test: $page_title</title>
</head>
<body>
$buffer
</body>
</html>
HTML;

echo $html;

?>
